package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.MyFileService;
import cn.bdqn.pojo.Know;
import cn.bdqn.pojo.MyFile;
import cn.bdqn.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.data.repository.query.Param;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的工作台-我的文件
 */
@Controller
public class MyFileController {
    @Autowired
    MyFileService myFileService;

    @RequestMapping("article-list3")
    public String getfile(Model model,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<MyFile> fileList = myFileService.getfile(user.getId());
        List<MyFile> fileList1 = new ArrayList<>();
        for (MyFile myFile : fileList) {
            String s = myFile.getRealFileName().substring(0, myFile.getRealFileName().lastIndexOf("."));
            if (s!=null){
                myFile.setRealFileName(s);
                fileList1.add(myFile);
            }
        }
        model.addAttribute("fileList",fileList1);
        return "article-list3";
    }

    @RequestMapping("file-add")
    public String file_add(){
        return "file-add";
    }

    /**
     * 上传
     * @param file
     * @param information
     * @param request
     * @return
     */
    @RequestMapping("myfileUpload")
    @ResponseBody
    public String  myfileUpload(@Param("file") MultipartFile file, String information, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        MyFile myFile = myFileService.myfileUpload(file, information, user);
        if (myFile!=null)
        return "200";
        return "error";
    }

    /**
     * 下载
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("download")
    @ResponseBody
    public ResponseEntity download(Integer id,HttpServletRequest request){
        MyFile myFile = this.myFileService.getFileInfoById(id.longValue());
        Resource resource = this.myFileService.loadFileAsResource(id.longValue());

        String contentType = null;
        try {
            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (contentType == null) {
            contentType = "application/octet-stream";
        }
        try {
            return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
                    .header("Content-Disposition",
                            "attachment; filename=\""
                                    + new String(myFile.getRealFileName().getBytes("GB2312"), "ISO8859-1") + "\"")
                    .body(resource);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping({ "/delFile" })
    @ResponseBody
    public String removeFile(Integer[] ids) {
        this.myFileService.removeFile(ids);
        return "删除成功";
    }
}
